/**
 * @author mrdoob / http://mrdoob.com/
 */

var Toolbar = function () {
    var panel = new UI.Panel().attr('id', 'toolbar');
    
    $(document.body).append(panel.node);

	var buttons = new UI.Panel();
	panel.add( buttons );

	// translate / rotate / scale
	var translate = new UI.Button('translate').attr('class', 'Button selected').css('position', 'relative');
	translate.attr('title', 'W');
	translate.bind('click', function () {
		Editor.Signal_TransformMode.dispatch( 'translate' );
	});

	var rotate = new UI.Button('rotate').css('position', 'relative');
	rotate.attr('title', 'E');
	rotate.bind('click', function () {
		Editor.Signal_TransformMode.dispatch( 'rotate' );
	} );

	var scale = new UI.Button('scale').css('position', 'relative');
	rotate.attr('title', 'R');
	scale.bind('click', function () {
		Editor.Signal_TransformMode.dispatch( 'scale' );
    } );
    
    buttons.add( translate, rotate, scale );

    var obj = null;

	Editor.Signal_TransformMode.add( function ( mode ) {
		if (obj instanceof Halo.Light) {
			translate.addClass( 'selected' );
			rotate.removeClass( 'selected' );
			scale.removeClass( 'selected' );
		} else {
			translate.removeClass( 'selected' );
			rotate.removeClass( 'selected' );
			scale.removeClass( 'selected' );
			switch ( mode ) {
				case 'translate': translate.addClass( 'selected' ); break;
				case 'rotate': rotate.addClass( 'selected' ); break;
				case 'scale': scale.addClass( 'selected' ); break;
			}
		}
	} );

	// grid
	var grid = new UI.Number( 100 ).css('width', '40px' ).bind('change', update );
	var snap = new UI.Boolean( false, 'snap' ).css('position', 'relative').bind('change', update );
	var local = new UI.Boolean( false, 'local' ).css('position', 'relative').bind('change', update );
	var showGrid = new UI.Boolean( true, 'grid' ).css('position', 'relative').bind('change', update );
    var progress = new UI.Text('');

    buttons.add(new UI.Text( 'grid: ' ), grid, snap, local, showGrid, progress);

    Editor.Signal_UpdateProgress.add( function ( text ) {
        progress.text(text);
    });

    Editor.Signal_Select.add(function(sel){
        obj = sel;
    });

	function update() {
        Editor.Signal_Grid.dispatch(snap.val(), grid.val(), local.val(), showGrid.val());
    }
};
